Jenkin 部署筆記


Posted by linglingDontCook on 2024-06-26

背景

主要記錄一些使用jenkin pipeline 部署 不同環境例如:dev,uat 時的一些知識

01 大致流程

主要寫三個stages

  • building stage
  • deploy stage
  • health check stage

02 WaitUntil 指令

  timeout(5) {
                        waitUntil {
                            def r = sh script: "wget -q ${host} -O /healthCheck", returnStatus: true
                            return r == 0
                        }
                    }

waitUntil 是一個Jenkin Pipeline 的command ,會不斷執行body内的動作,直到return true , 搭配timeout 使用可以用來檢查系統是否有成功部署.

The waitUntil command in Jenkins Pipeline is used to create a loop that will continue to execute its body until the body returns true. It's often used when you need to wait for a certain condition to be met before proceeding with the rest of the pipeline.

In the provided script, the waitUntil command is used to repeatedly execute a wget command until it succeeds. The wget command is used to download a file from a specific URL, and it returns an exit status of 0 if it succeeds and a non-zero exit status if it fails.

The sh step is used to execute the wget command, and the returnStatus: true option is used to make the sh step return the exit status of the command. The exit status is stored in a variable named r.

The return r == 0 line is the condition that the waitUntil command is waiting for. If the wget command succeeds and r is 0, this line will return true and the waitUntil command will stop looping. If the wget command fails and r is not 0, this line will return false and the waitUntil command will continue to loop.

The timeout(5) command is used to limit the amount of time that the waitUntil command can loop for. If the waitUntil command is still looping after 5 minutes, the timeout command will cause the pipeline to fail.

03 SSH 相關指令

sshCommand remote: remote (map) , sudo:boolean , command: """ """

sshCommand 是 Jenkins Pipeline 的一個步驟,用於在遠程服務器上執行命令。這個步驟接受一些參數來配置其行為。

在 sshCommand remote: remote, sudo: false, command: """ """ 中:

remote: remote 是一個參數,指定了要在哪個遠程服務器上執行命令。remote 是一個 map,通常包含如主機名、用戶名和密碼等信息。

sudo: false 是一個參數,指定是否以超級用戶權限運行命令。如果設置為 true,則命令將以超級用戶權限運行。如果設置為 false,則命令將以普通用戶權限運行。

command: """ """ 是一個參數,指定了要執行的命令。命令應該被包含在三個引號之間,這樣就可以包含多行文本。

sshPutsshGet 是 Jenkins Pipeline 的步驟,用於在遠程服務器上上傳和下載文件。

  1. sshPut 的基本語法如下:
sshPut remote: <remote>, from: <localPath>, into: <remotePath>
  • remote: <remote> 指定了要上傳文件的遠程服務器。<remote> 是一個 map,通常包含如主機名、用戶名和密碼等信息。
  • from: <localPath> 指定了要上傳的本地文件或目錄的路徑。
  • into: <remotePath> 指定了遠程服務器上的目標路徑。
  1. sshGet 的基本語法如下:
sshGet remote: <remote>, from: <remotePath>, into: <localPath>
  • remote: <remote> 指定了要從哪個遠程服務器下載文件。<remote> 是一個 map,通常包含如主機名、用戶名和密碼等信息。
  • from: <remotePath> 指定了要下載的遠程文件或目錄的路徑。
  • into: <localPath> 指定了本地的目標路徑。

04 Jekins Pipeline 的 stage , stepscript

在 Jenkins Pipeline 中,stage, steps, 和 script 是用來組織和控制流程的關鍵結構。

  1. stage: 一個 stage 是 Pipeline 中的一個階段,通常用於表示 CI/CD 流程中的一個重要階段。例如,你可能有一個名為 "Build" 的 stage,一個名為 "Test" 的 stage,和一個名為 "Deploy" 的 stage。每個 stage 都會在 Jenkins 的 UI 中顯示,使得流程的進度和結果一目了然。

  2. steps: steps 是在 stage 中執行的操作。每個 step 是一個單獨的任務,可以是一個簡單的命令,也可以是一個複雜的腳本或者是一個外部插件的功能。例如,一個 "Build" stage 可能包含了 "Checkout code"、"Compile code"、"Run unit tests" 等 steps

  3. script: script 是一個特殊的 step,用於執行 Groovy 腳本。在 script 區塊中,你可以使用更複雜的邏輯,例如條件語句、迴圈等。這對於需要更複雜控制流程的情況非常有用。

總的來說,stagestepsscript 是用來組織和控制 Jenkins Pipeline 的工具。stage 是流程的大階段,steps 是每個階段中的具體操作,而 script 則提供了更複雜的控制流程的能力。


#Jenkin #devops #CI/CD







Related Posts

一看就懂的 React Native + Firebase Mobile App 入門教學

一看就懂的 React Native + Firebase Mobile App 入門教學

[SQL] 兩組日期區間進行比較

[SQL] 兩組日期區間進行比較

[day-3]布林、undefined、null/字串數字轉換/比較與邏輯運算子

[day-3]布林、undefined、null/字串數字轉換/比較與邏輯運算子


Comments